package controllers

import (
	"encoding/json"
	"time"
	"weiboxs.com/new_h_glasses/enums"
	"weiboxs.com/new_h_glasses/models"
	"weiboxs.com/new_h_glasses/utils"
)

type FollowupController struct {
	BaseController
}

func (c *FollowupController) Prepare() {
	c.BaseController.Prepare()
}

func (c *FollowupController) DataGrid() {
	c.checkLogin()
	var params models.FollowupQueryParam
	json.Unmarshal(c.Ctx.Input.RequestBody, &params)

	data, total := models.FollowupPageList(&params)
	c.jsonDataTable(total, data)
}

//新增随访
func (c *FollowupController) Add() {
	c.checkLogin()

	if c.Ctx.Input.IsPost() {
		model := &models.Followup{}

		//model.CreatedAt = utils.GetDateParse(c.GetString("crdate"))
		model.Message = c.GetString("message")

		if len(model.Message) <= 0 {
			c.jsonResult(enums.JRCodeFailed, "请填写随访内容", "")
		}
		archives_id,_ := c.GetInt64("archives")
		archives := &models.Archives{Id:archives_id}
		model.Archives = archives

		if _, err := models.FollowupAdd(model);err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
			return
		}

		c.jsonResult(enums.JRCodeSucc, "随访保存成功", "")
	}  else {
		c.Data["date"] = utils.GetDateFormat(time.Now().Unix(), "2006-01-02")
 		c.display("", "followup/add_footerjs.html", "shared/layout_dialog.html")
	}
}

//删除随访
func (c *FollowupController) Delete() {
	c.checkLogin()

	id,_ := c.GetInt64("id")
	if id == 0 {
		c.jsonResult(enums.JRCodeFailed, "参数错误", "")
		return
	}
	if err := models.FollowupDeleteById(id); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		return
	}
	c.jsonResult(enums.JRCodeSucc, "删除成功", "")
}
